﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WcfBackend;

public partial class _Default : System.Web.UI.Page
{
    private HttpCookie userCookie;
    private WcfBackend.WcfFoodAndAllergiesClient wcf;

    protected void Page_Load(object sender, EventArgs e)
    {
        string userName;
        string userPass;
        lblStatus.Visible = false;
        wcf = new WcfFoodAndAllergiesClient();

        if (Request.Cookies["userInfo"] != null)
        {
            if (Request.Cookies["userInfo"]["userName"] != null)
            {
                userName = Request.Cookies["userInfo"]["userName"];
                userPass = Request.Cookies["userInfo"]["userPassword"];
                try
                {
                    User u = wcf.GetUserByUserNameAndPassword(userName, userPass);
                    if (u.UserName.Equals(userName) && u.UserPassword.Equals(userPass))
                    {
                        lblStatus.Visible = true;
                        lblStatus.Text = userName;
                        //Response.Redirect("~/RecipeSearch.aspx");
                    }
                }
                catch
                {
                    lblStatus.Visible = true;
                    lblStatus.Text = "Der skete en fejl ved læsning af din Cookie. Prøv igen eller slet dine cookies.";
                    //lblStatus.Text = ex.ToString();
                    lblStatus.CssClass = "label-warning";
                }
            }
        }
        else
        {
            userCookie = new HttpCookie("userInfo");
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(txtUserName.Text) && !String.IsNullOrEmpty(txtUserPassword.Text))
        {

            string name = txtUserName.Text;
            string passw = txtUserPassword.Text;
            try
            {
                User u = wcf.GetUserByUserNameAndPassword(name, passw);
                if (u.UserName.Equals(name) && u.UserPassword.Equals(passw))
                {
                    Session["user"] = u;
                    //Response.Redirect("~/RecipeSearch.aspx");
                    Response.Redirect("Userprofile.aspx");
                }
                else
                {
                    lblStatus.Visible = true;
                    lblStatus.Text = "Forkert brugernavn eller kodeord!";
                    lblStatus.CssClass = "label-warning";
                }

            }
            catch (Exception ex)
            {
                lblStatus.Visible = true;
                lblStatus.Text = ex.ToString();
                lblStatus.CssClass = "label-warning";
            }
        }
        else
        {
            lblStatus.Visible = true;
            lblStatus.Text = "Der mangler bruger oplysninger!";
            lblStatus.CssClass = "label-warning";
        }
    }

    protected void lnkCreation_Click(object sender, EventArgs e)
    {
        lblStatus.Visible = false;
        mView.SetActiveView(v2);
    }

    protected void lnkLogin_Click(object sender, EventArgs e)
    {
        lblStatus.Visible = false;
        mView.SetActiveView(v1);
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        string name = txtNewName.Text;
        string passw = txtNewPassword.Text;
        int allergies = 0;
        List<Allergies> temList = new List<Allergies>();
        try
        {
            List<ListItem> selectedItems = cblAllergies.Items.Cast<ListItem>().Where(li => li.Selected).ToList();

            if (!String.IsNullOrEmpty(txtNewName.Text) && !String.IsNullOrEmpty(txtNewPassword.Text) && selectedItems.Count > 0)
            {
                for (int i = 0; i < selectedItems.Count; i++)
                {

                    allergies = Int32.Parse(selectedItems[i].Value);
                        temList.Add(wcf.GetAllergiesById(allergies));
                }
                userCookie["userName"] = name;
                userCookie["userPassword"] = passw;
                Response.Cookies.Add(userCookie);
                User u = new User();
                u.UserName = name;
                u.UserPassword = passw;

                u.UserAllergies = temList.ToArray();
                int t;
                t = wcf.InsertUser(u);
                if (t > 0)
                {
                    lblStatus.Visible = true;
                    lblStatus.Text = "Success";
                    lblStatus.CssClass = "label-success";
                }
                else
                {
                    lblStatus.Visible = true;
                    lblStatus.Text = "Der var problemer med uploading. Prøv igen.";
                    lblStatus.CssClass = "label-warning";
                }
            }
            else
            {
                lblStatus.Visible = true;
                lblStatus.Text = "Der mangler nogen oplysninger!";
                lblStatus.CssClass = "label-warning";
            }

        }
        catch (Exception ex)
        {
            lblStatus.Visible = true;
            lblStatus.Text = ex.ToString();
            lblStatus.CssClass = "label-warning";
        }
    }
    protected void lnkDeleteCookies_Click(object sender, EventArgs e)
    {
        HttpCookie userCookie = new HttpCookie("userInfo");
        userCookie.Expires = DateTime.Now.AddDays(-1d);
        Response.Cookies.Add(userCookie);
    }
}